package leetcode.offer;

import leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;

/**
 * ClassName: BinaryTreeLevelOrderPrint
 * Description: leetcode-剑指offer 32 - I. 从上到下打印二叉树
 * date: 2021-11-02 8:48
 *
 * @author liyifan
 */
public class BinaryTreeLevelOrderPrint {
    public int[] levelOrder(TreeNode root) {
        if (root == null) {
            return new int[]{};
        }
        ArrayList<Integer> res = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode temp = queue.removeFirst();
            res.add(temp.val);
            if (temp.left != null)
                queue.add(temp.left);
            if (temp.right != null)
                queue.add(temp.right);
        }
        int[] result = new int[res.size()];
        for (int i = 0;i < res.size();i++) {
            result[i] = res.get(i);
        }
        return result;
    }
}
